package com.emergency.map;

import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Rectangle;

/**
 * 如何实现按距离排序、范围查找
 * @author htmic
 * @date 2020/9/17
 */
public class testMap {

    /**
     *
     * 参考资料 https://blog.csdn.net/ghsau/article/details/50591932
     * @param args
     */
    public static void main(String[] args) {
        //范围搜索
        // 移动设备经纬度
        double lon = 119.95215246520607, lat = 30.55734777118395;
        // 千米
        int radius = 8;
        SpatialContext geo = SpatialContext.GEO;
        Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(geo.makePoint(lon, lat), radius * DistanceUtils.KM_TO_DEG, geo, null);
        System.out.println(rectangle.getMinX() + "-" + rectangle.getMaxX());// 经度范围
        System.out.println(rectangle.getMinY() + "-" + rectangle.getMaxY());// 纬度范围

        //sql 查询
        /**
         *         SELECT * FROM customer
         *         WHERE (lon BETWEEN ? AND ?) AND (lat BETWEEN ? AND ?);
         *
         *         需要给lon、lat两个字段建立联合索引：INDEX `idx_lon_lat` (`lon`, `lat`)
         */

        //过滤-距离排序
        // 移动设备经纬度
        double lon1 = 116.3125333347639, lat1 = 39.98355521792821;
        // 商户经纬度
        double lon2 = 116.312528, lat2 = 39.983733;
        SpatialContext geo1 = SpatialContext.GEO;
        //离中心点距离
        double distance = geo1.calcDistance(geo.makePoint(lon1, lat1), geo1.makePoint(lon2, lat2))* DistanceUtils.DEG_TO_KM;
        System.out.println(distance);// KM
        //通过stream()排序
//        Collections.sort(list, comparator)
    }
}
